package pearson.automation.forms.courseDetails;

import java.util.Calendar;

import org.openqa.selenium.By;
import org.testng.Assert;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ComboBox;
import framework.elements.DateCalendar;
import framework.elements.Label;
import framework.elements.TextBox;
/**
 * Allow to modify Course's settings. Location:
 * CourseForm->CourseDetails
 */
public class CourseDetailsForm extends BaseForm {
    protected TextBox title = new TextBox(By.id("course_title"), "Title");
    protected TextBox edition = new TextBox(By.id("course_edition"), "Edition");
    protected TextBox subject = new TextBox(By.id("course_subject"), "Subject");
    protected TextBox courseNumber = new TextBox(By.id("course_number"), "Course number");
    protected ComboBox term = new ComboBox(By.id("term"), "Term");
    protected ComboBox privacy = new ComboBox(By.id("privacy"), "Privacy");
    protected TextBox startDate = new TextBox(By.id("start_date"), "Start Date");
    protected DateCalendar calendar = new DateCalendar(By.tagName("button"), "button");

    protected TextBox description = new TextBox(By.tagName("textarea"), "Description");
    protected TextBox tagsInput = new TextBox(By.className("textboxlist-bit-editable-input"), "Tags");
    protected Label   tagsOutput = new Label(By.className("textboxlist-bits"), "Tags Output");
    protected Button  tagDeleteButton = new Button(By.className("textboxlist-bit-box-deletebutton"), "Tag Delete button");

    protected static Button save = new Button(By.name("_submit"), "Save");
    private static Button cancel = new Button(By.linkText("Cancel"), "Cancel");
    private Button close = new Button(By.linkText("Close"), "Close");

    public CourseDetailsForm() {
    super(save.getLocator(), "Publish");
    }

    protected CourseDetailsForm(By titleLocator, String title) {
    super(titleLocator, title);
    }

    public void setTitle(String title) {
    this.title.setText(title);
    }

    public String getTitle() {
    return title.getValue();
    }

    public void setEdition(String edition) {
    this.edition.setText(edition);
    }

    public String getEdition() {
    return edition.getValue();
    }

    public void setSubject(String subject) {
    this.subject.setText(subject);
    }

    public String getSubject() {
    return subject.getValue();
    }

    public void setTerm(String term) {
    this.term.selectByText(term);
    }

    public String getTerm() {
    return term.getSelectedLabel();
    }

    public void setPrivacy(String privacy) {
    this.privacy.selectByText(privacy);
    }

    public String getPrivacy() {
    return privacy.getSelectedLabel();
    }

    /**
     * Instead of setNextValidDate
     * 
     * @param date
     *            - date to choose (parse in days\months\year automatically)
     */
    public void setValidDate(Calendar date) {
    calendar.selectDate(date);
    }

    public String getStartDate() {
    return startDate.getValue();
    }

    public void setDescription(String description) {
    this.description.setText(description);
    }

    public String getDescription() {
    return description.getValue();
    }

    public void setTags(String tags) {
        //Clear all tags here
        while (tagDeleteButton.isPresent()) {
            tagDeleteButton.clickAndWait();
        }
        this.tagsInput.setText(tags);
    }

    public String getTags() {
    return tagsOutput.getText().replace("\n", " ");
    }

    public void setCourseNumber(String courseNum) {
    courseNumber.setText(courseNum);
    }

    public String getCourseNumber() {
    return courseNumber.getValue();
    }

    public CourseDetailsSavedMessageForm clickSave() {
    save.click();
    return new CourseDetailsSavedMessageForm();
    }

    public CourseForm clickCancel() {
    cancel.clickAndWait();
    return new CourseForm();
    }

    public void checkCourseDetails(String title, String subject, String description, String edition, String privacy,
        String startDate, String tags, String term, String courseNum) {
    info("Checking course details");
    Assert.assertEquals(getTitle(), title, "Title is wrong");
    Assert.assertEquals(getSubject(), subject, "Subject is wrong");
    Assert.assertEquals(getDescription(), description, "Description is wrong");
    Assert.assertEquals(getEdition(), edition, "Edition is wrong");
    Assert.assertEquals(getPrivacy(), privacy, "Privacy is wrong");
    Assert.assertEquals(getStartDate(), startDate, "Start date is wrong");
    Assert.assertEquals(getTags(), tags, "Tags are wrong");
    Assert.assertEquals(getTerm(), term, "Term is wrong");
    Assert.assertEquals(getCourseNumber(), courseNum, "Course number is wrong");
    }

    /**
     * Setting new course details
     * @param title
     * @param subject
     * @param description
     * @param edition
     * @param privacy
     * @param startDate
     * Count of days to be added to the current date
     * @param tags
     * @param term
     * @param courseNum
     */
    public void updateCourseDetails(String title, String subject, String description, String edition, String privacy,
        int startDate, String tags, String term, String courseNum) {
    info("Updating course info");
    setTitle(title);
    setSubject(subject);
    setDescription(description);
    setEdition(edition);
    setPrivacy(privacy);
    Calendar date = Calendar.getInstance();
    date.add(Calendar.DAY_OF_MONTH, startDate);
    setValidDate(date);
    setTags(tags);
    setTerm(term);
    setCourseNumber(courseNum);
    checkCourseDetails(title, subject, description, edition, privacy, getStartDate(), tags, term, courseNum);
    }

    public CourseForm clickClose() {
    close.clickAndWait();
    return new CourseForm();
    }

}
